
package com.yimixian.app.ui;

import android.app.Activity;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.yimixian.app.R;
import com.yimixian.app.SystemFramework;
import com.yimixian.app.activity.YMXMainActivity;
import com.yimixian.app.cart.CartManager;
import com.yimixian.app.common.UMengConstants;
import com.yimixian.app.goods.GoodDetailActivity;
import com.yimixian.app.goods.GoodDetailTopFragment;
import com.yimixian.app.model.CartUpdate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by songzhiyang on 2015/8/29.
 */
public class DetailCartView extends FrameLayout {

    private Context mContext;
    private TextView mTotalSimpleDescription;
    private AnimationFrameLayout mCart;
    private FrameLayout mPlusMinusContainer;
    private ImageView cart_shop;
    public TextView getTotalSimpleDescription(){
        return mTotalSimpleDescription;
    }
    private Button btn_go_shangcheng;
    private LinearLayout ll_go_shangcheng;

    public DetailCartView(Context context) {
        super(context);
        this.mContext = context;
        initView();
    }

    public DetailCartView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        initView();
    }

    public DetailCartView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initView();
    }

    public void initView() {
        View view = View.inflate(mContext, R.layout.detail_cart_view, this);
        mTotalSimpleDescription = (TextView) view.findViewById(R.id.cart_contnent);
        mCart = (AnimationFrameLayout) view.findViewById(R.id.cart);
        cart_shop = (ImageView) view.findViewById(R.id.cart_shop);
        btn_go_shangcheng = (Button) view.findViewById(R.id.btn_go_shangcheng);
        ll_go_shangcheng = (LinearLayout) view.findViewById(R.id.ll_go_shangcheng);
        mPlusMinusContainer = (FrameLayout) view.findViewById(R.id.plus_minus_icon_container);
        cart_shop.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                MobclickAgent.onEvent(SystemFramework.getInstance().getGlobalContext(),
                        UMengConstants.NEW_GOODDETAIL_CATEGORYBTN);
                GoodDetailActivity mActivity = (GoodDetailActivity) getContext();
                mActivity.finishWithResult(GoodDetailTopFragment.RETURN_TO_CATEGORY);
            }
        });
        btn_go_shangcheng.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                MobclickAgent.onEvent(SystemFramework.getInstance().getGlobalContext(),
                        UMengConstants.NEW_GOODDETAIL_POPCATEGORYBTN);
                GoodDetailActivity mActivity = (GoodDetailActivity) getContext();
                mActivity.finishWithResult(GoodDetailTopFragment.RETURN_TO_CATEGORY);
            }
        });
    }

    public boolean isBadgeNumberIsNull() {
        if (mTotalSimpleDescription == null) {
            return true;
        } else {
            return false;
        }
    }

    public void setBadgeIcon(int number) {
        CartUpdate cartUpdate = CartManager.getInstance().mCartUpdate;
        if(CartManager.getInstance().isUserLogin()){
            if (cartUpdate != null && cartUpdate.total != null
                && !TextUtils.isEmpty(cartUpdate.total.totalPrice) && mTotalSimpleDescription != null) {
                if (cartUpdate.total.count <= 0) mTotalSimpleDescription.setVisibility(View.GONE);
                else{
                    mTotalSimpleDescription.setVisibility(View.VISIBLE);
                    mTotalSimpleDescription.setText(cartUpdate.total.totalPrice);
                }
            } else {
                mTotalSimpleDescription.setVisibility(View.GONE);
            }
        }else{
            CartManager.getInstance().setCartQuantityText();
        }


        // boolean mHasGoods = number > 0;
        // mTotalSimpleDescription.setVisibility(mHasGoods ? View.VISIBLE :
        // View.INVISIBLE);
    }

    public AnimationFrameLayout getCartViewIcon() {
        return mCart;
    }

    public FrameLayout getPlusMinusContainer() {
        return mPlusMinusContainer;
    }
}
